package sat.component;

import org.mt4j.MTApplication;
import org.mt4j.components.visibleComponents.font.FontManager;
import org.mt4j.components.visibleComponents.shapes.MTRectangle;
import org.mt4j.components.visibleComponents.widgets.MTTextField;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
import org.mt4j.util.MTColor;
import org.mt4jx.components.visibleComponents.widgets.toolbar.MTLayoutContainer;

public class ToolbarTab extends MTRectangle{

	private MTApplication app;
	private Toolbar toolBar;
	private MTLayoutContainer container;
	private MTTextField tabName;
	
	private final MTColor vert = new MTColor(122, 142, 117, 255);
	private final MTColor marron = new MTColor(164, 117, 91, 255);
	private final MTColor jaune = new MTColor(215, 195, 168, 255);

	public MTLayoutContainer getContainer() {
		return container;
	}
	
	public void setContainer(MTLayoutContainer container) {
		this.container = container;
	}
	
	public MTTextField getTabName() {
		return tabName;
	}

	public void setTabName(MTTextField tabName) {
		this.tabName = tabName;
	}

	public void setSelected(){
		this.setFillColor(marron);
		this.setStrokeColor(vert);
		tabName.setFontColor(jaune);
	}
	
	public void setUnselected(){
		this.setFillColor(vert);
		this.setStrokeColor(marron);
		tabName.setFontColor(jaune);
		
	}
	
	public void toggle(){
		toolBar.getTabArray().setAllUnselected();
		toolBar.changeCurrentContainer(container);
		setSelected();
	}
	
	public ToolbarTab(MTApplication mtApplication,Toolbar tb,float width, float height, String name) {
		super(mtApplication, 0, 0, 0, width, height);
		this.app = mtApplication;
		this.toolBar = tb;
		this.setFillColor(vert);
		this.setStrokeColor(marron);
		this.setStrokeWeight(3);
		
		//implement gesture for tab
		//this.unregisterAllInputProcessors(); //Remove the default drag, rotate and scale gestures first
	
		tabName = new MTTextField(mtApplication, 0, 0, this.getWidthXYGlobal(), this.getHeightXYGlobal(), 
				FontManager.getInstance().createFont(mtApplication, "Comic Sans MS",
						20, // Font size
						jaune, // Font fill color
						true));

		// Since the TextArea is a rectangle we only want to show the text
		// So we set properties which will hide the rectangle
		tabName.setNoFill(true);
		tabName.setNoStroke(true);
		
		// Set the Text in this component
		tabName.setText(name);
		tabName.setPickable(false);
		
		// set object name
		this.setName(name);
		
		// implement gesture for text 
		this.unregisterAllInputProcessors();
		this.registerInputProcessor(new TapProcessor(this.app));
		this.addGestureListener(TapProcessor.class, new IGestureEventListener() {
			@Override
			public boolean processGestureEvent(MTGestureEvent ge) {
				// TODO Auto-generated method stub
				TapEvent te = (TapEvent)ge;
					switch (te.getTapID()) {
					case TapEvent.TAPPED:
						toggle();
						break;
					default:
						break;
					}
				return false;
			}
		});
		this.addChild(tabName);
	}
}
